package cn.pxs.service;

import cn.pxs.dao.BlogsDao;
import cn.pxs.dao.ContentDao;
import cn.pxs.entity.Blogs;
import cn.pxs.entity.BlogsAndContent;
import cn.pxs.entity.Content;
import cn.pxs.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BlogsServiceImpl implements BlogsService{
    @Autowired
    BlogsDao dao;
    @Autowired
    ContentDao contentDao;

    @Override
    public void add(Map map) {
        Content content = new Content();
        Blogs blogs = new Blogs();
        content.setBody((String) map.get("body"));
        blogs.setText((String) map.get("text"));
        blogs.setTitle((String) map.get("title"));
        blogs.setHits((Integer) map.get("hits"));
        blogs.setTypeid((String) map.get("typeid"));
        blogs.setDate(new Date());
        blogs.setAuthor(Tools.getUser().getUsername());
        blogs.setUid(Tools.getUser().getId());
//        System.out.println("......"+content);
        Content c = contentDao.add(content);
        blogs.setContentid(c.getId());
        dao.add(blogs);
    }

    @Override
    public void delById(String id) {
        //内容一起删掉
        Blogs b = dao.delById(id);
        contentDao.delById(b.getContentid());
    }

    public List<Blogs> findBlogs(){
        List<Blogs> bs = dao.findAll();
        return bs;
    }


    @Override
    public BlogsAndContent findById(String id) {
        Blogs b = dao.findById(id);
        Content c = contentDao.findById(b.getContentid());
        BlogsAndContent bc = new BlogsAndContent();
        bc.setContent(c);
        bc.setBlogs(b);
        return bc;
    }

    @Override
    public List<Blogs> findByTypeId(String typeid) {
//        System.out.println(typeid);
        List<Blogs> bs = dao.findByTypeId(typeid);
        return bs;
    }

    @Override
    public List<Blogs> findByUid(String uid) {
        List<Blogs> bs = dao.findByUid(uid);
        return bs;
    }

    @Override
    public List<Blogs> findByTypeIdAndUid(String typeid, String uid) {
        List<Blogs> bs = dao.findByTypeIdAndUid(typeid,uid);
        return bs;
    }
}
